package com.koron.order.workbench.todoCenter.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.koron.bean.app.AppBean;
import com.koron.bean.base.Response;
import com.koron.common.core.business.common.service.SystemCommonService;
import com.koron.common.core.business.workflow.bean.param.BaseParam;
import com.koron.common.core.business.workflow.bean.param.ListParam;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.order.workbench.todoCenter.bean.vo.TodoVo;
import com.koron.order.workbench.todoCenter.service.TodoService;
import com.koron.order.workbench.websocket.WebSocketServer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.ADOConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName TodoController
 * @description
 * @Author zhouj
 * @Date 2022/2/10 17:51
 */
@Slf4j
@Api(tags = "待办中心(todoCenter)")
@RestController
@RequestMapping("/todo/")
public class TodoController {

    @Autowired
    WebSocketServer webSocketServer;

    @Autowired
    SystemCommonService systemCommonService;

    private String getCurrDs() {
        return ThreadLocalContext.get().getCurrDs();
    }

    @Autowired
    TodoService todoService;

    @ApiOperation(value = "获取待办列表(type区分待办,已办)")
    @GetMapping("getUndoList")
    public Response<PageInfo> getUndoList(ListParam listParam, int pageSize) {

        if (pageSize > 0) {
            listParam.setPageCount(pageSize);
        }
        //获取待办列表
        return Response.ok(ADOConnection.runTask(getCurrDs(), todoService, "getUndoListPage", PageInfo.class, listParam));
    }

    @ApiOperation(value = "页面获取待办分组数量")
    @GetMapping("getUndoGroup")
    public Response<List> getUndoGroup(ListParam listParam) {
        //获取待办列表
        return Response.ok(ADOConnection.runTask(getCurrDs(), todoService, "getUndoGroup", List.class, listParam));
    }

    @ApiOperation(value = "websocket推送客户端待办分组数量--简化版")
    @GetMapping("pushUndoGroupSigle")
    public Response<String> pushUndoGroupSigle(@RequestParam("account") String account, @RequestParam("currDs") String currDs) {
        //推送待办列表
        ArrayList<String> list = new ArrayList<>();
        list.add(account);

        return pushUndoGroupSimple(list, currDs);
    }

    @ApiOperation(value = "websocket推送客户端待办分组数量--简化版多账号")
    @GetMapping("pushUndoGroupSimple")
    public Response<String> pushUndoGroupSimple(@RequestParam("accountList") List<String> accountList, @RequestParam("currDs") String currDs) {
        //推送待办列表
        return pushUndoGroup(accountList, currDs, new ListParam());
    }

    @ApiOperation(value = "websocket推送客户端待办分组数量")
    @GetMapping("pushUndoGroup")
    @Async
    public Response<String> pushUndoGroup(@RequestParam("accountList") List<String> accountList, @RequestParam("currDs") String currDs, ListParam listParam) {
        //推送待办列表
        Assert.isTrue(CollectionUtils.isNotEmpty(accountList), "账号不能为空");

        AppBean appBean = systemCommonService.queryAppBeanByMark(currDs);

        Assert.isTrue(appBean != null, "appBean不能为空");

        ThreadLocalContext.setApp(appBean);
        ThreadLocalContext.get().setCurrDs(appBean.getMark());

        accountList.forEach(p -> {
            listParam.setOperator(p);
            if (StringUtils.isNotEmpty(currDs)) {
                BaseParam baseParam = new BaseParam();
                baseParam.setAppCode(appBean.getCode());
                listParam.setBaseParam(baseParam);
            }

            List<TodoVo> todoVoList = ADOConnection.runTask(appBean.getMark(), todoService, "getUndoGroup", List.class, listParam);
            webSocketServer.sendToUser(p, JSON.toJSONString(Response.ok(todoVoList)));
        });
        //清除ThreadLocalContext线程信息,防止污染
        ThreadLocalContext.clear();

        return Response.ok("推送完成");
    }

}
